package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/27 9:56
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public PageInfo<ConsumerVO> findConsumerList(String pageNum, String pageSize, ConsumerVO consumerVO) {
        if(!StringUtils.isEmpty(pageNum)&&!StringUtils.isEmpty(pageSize)){
            PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        }
        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        if(consumerVO!=null){
            if(!StringUtils.isEmpty(consumerVO.getName())){
                criteria.andLike("name","%"+consumerVO.getName()+"%");
            }
            if(!StringUtils.isEmpty(consumerVO.getAddress())){
                criteria.andLike("address","%"+consumerVO.getAddress()+"%");
            }
            if(!StringUtils.isEmpty(consumerVO.getContact())){
                criteria.andLike("contact","%"+consumerVO.getContact()+"%");
            }
        }
        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        List<ConsumerVO> consumerVOList = new ArrayList<>();
        for (Consumer consumer : consumerList) {
            ConsumerVO consumerResVo = new ConsumerVO();
            BeanUtils.copyProperties(consumer,consumerResVo);
            consumerVOList.add(consumerResVo);
        }
        return new PageInfo<ConsumerVO>(consumerVOList);
    }

    @Override
    public Consumer add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
        return consumer;
    }

    @Override
    public ConsumerVO edit(Long id) {
        ConsumerVO consumerVO = new ConsumerVO();
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(consumer,consumerVO);
        return consumerVO;
    }

    @Override
    public void update(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer);
    }

    @Override
    public void delete(Long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }
}
